package com.tjbklx1.zk.testcurator;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.RetryUntilElapsed;
import org.apache.zookeeper.data.Stat;

public class CheckExists {

	public static void main(String[] args) throws Exception {

		ExecutorService es = Executors.newFixedThreadPool(5);

		RetryPolicy retryPolicy = new RetryUntilElapsed(5000, 1000);

		CuratorFramework client = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181")
				.sessionTimeoutMs(5000).connectionTimeoutMs(5000).retryPolicy(retryPolicy).build();

		client.start();
		
		//非异步操作
//		Stat s = client.checkExists().forPath("/abc");
//		System.out.println(s);
		
		
		//异步操作
		client.checkExists().inBackground(new BackgroundCallback() {
			public void processResult(CuratorFramework arg0, CuratorEvent arg1) throws Exception {
				Stat stat = arg1.getStat();
				System.out.println(stat);
				System.out.println(arg1.getContext());
				System.out.println(arg1.getPath());
				System.out.println(arg1.getChildren());
				System.out.println(arg1.getData());
			}
		}, "123", es).forPath("/abc");
		//"123" 上下文 context ,主要用于传递一些参数
		Thread.sleep(Integer.MAX_VALUE);
	}

}
